/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.osdtsystem.dataset;

import java.util.Arrays;
import java.util.List;
import org.osdtsystem.utils.CacheMap;

/**
 * Interface for data points in a data set.
 * @author Matthias Buch-Kromann
 */
public class DataPoint {
    // The features in the data point
    String[] features;

    public final static int cacheSize = 10000;

    final static CacheMap<String,String> cache
            = new CacheMap<String,String>(cacheSize);

    public DataPoint(String... features) {
        // Save features
        this.features = features;

        // Replace with identical strings in cache, if they exist
        for (int i = 0; i < features.length; ++i) {
            String cachedString = cache.get(features[i]);
            if (cachedString != null)
                features[i] = cachedString;
        }
    }

    public int dimensions() {
        return features.length;
    }
    
    /**
     * The features in the data point.
     * @return the features
     */
    public List<String> features() {
        return Arrays.asList(features);
    }

    /**
     * The ith feature in the data point.
     * @param i the feature index
     * @return the feature at the given feature index
     */
    public String feature(int i) {
        return features[i];
    }

    public double weight() {
        return 1;
    }

    @Override
    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append(weight());
        s.append(":[");
        boolean first = true;
        for (String feature : features) {
            if (! first)
                s.append("|");
            first = false;
            s.append(feature);
        }
        s.append("]");
        return s.toString();
    }
}
